package com.feihong.yw189.paidan.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.astuetz.PagerSlidingTabStrip;
import com.feihong.yw189.R;
import com.feihong.yw189.bluetooth.dialog.MyUsualyDialog;
import com.feihong.yw189.fragment.base.BaseFragment;
import com.feihong.yw189.paidan.activity.DispatchActivity;
import com.feihong.yw189.paidan.adapter.PaiDanOrderAdapter;
import com.feihong.yw189.paidan.view.NestingViewPager;

import java.util.HashMap;

/**
 * 历史订单
 * 张鹏
 * 2018.2
 */
public class DispatchList extends BaseFragment {
    private PagerSlidingTabStrip pagerSlidingTabStrip;
    private NestingViewPager viewPager;
    private DispatchActivity mActivity;

    private MyUsualyDialog progressDialog;
    private PaiDanOrderAdapter pagerAdapter;


    public static DispatchList newInstance(String tabName) {
        DispatchList rechargeFragment = new DispatchList();
        return rechargeFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (DispatchActivity) getActivity();
        progressDialog = new MyUsualyDialog(mActivity, R.layout.wait_item);
        progressDialog.setCancelable(false);
        progressDialog.setCanceledOnTouchOutside(false);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragmentView = inflater.inflate(R.layout.list_dispatch, container, false);
        return fragmentView;
    }

    @Override
    protected void setTheme() {
        isDrakFont = false;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        bindData();
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
//        if (isInit) {
//            bindData();
//        }
    }

    @Override
    public void onStop() {
        super.onStop();
        progressDialog.dismiss();
    }

    /**
     * 初始化布局
     */
    @Override
    public void initView() {
        pagerSlidingTabStrip = (PagerSlidingTabStrip) findView(R.id.order_tabs);
        viewPager = (NestingViewPager) findView(R.id.vp_order);
        pagerAdapter = new PaiDanOrderAdapter(getChildFragmentManager());
        numMap.put("0", "全部");
        numMap.put("1", "已完成");
        numMap.put("2", "已取消");
        if (viewPager != null && pagerAdapter != null) {
            pagerAdapter.setData(numMap);
            viewPager.setAdapter(pagerAdapter);
            pagerSlidingTabStrip.setIndicatorHeight(pagerAdapter.getCount());
            viewPager.setOffscreenPageLimit(2);
            pagerSlidingTabStrip.setViewPager(viewPager);
        }
    }

    private HashMap<String, String> numMap = new HashMap<>();

    @Override
    public void bindData() {
        pagerSlidingTabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                pagerAdapter.fragments.get(position).setPaiDanOrderAdapter(pagerAdapter);

            }

            @Override
            public void onPageSelected(final int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


}
